// pages/topicality/subtopic/subtopic.js
const app = getApp()
const util = require("../../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // cityname: app.globalData.extConfig.cityname,
        // citycode: app.globalData.extConfig.citycode,
        meunIndex: 0,
        slideWidth: '', //滑块宽
        slideLeft: 0, //滑块位置
        totalLength: '', //当前滚动列表总长
        slideShow: false,
        slideRatio: '',
        showtk: false, // 跳转外链的弹框显示
        showNav: true, // 快捷导航是否展开

        showItems: true, // 我要办理是否展开
        queryWidth: "",
        scrollTop: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var sceneData = [];
        if (options.scene != undefined) {
            var scene = decodeURIComponent(options.scene);
            var arrPara = scene.split("/");
            for (var i in arrPara) {
                sceneData.push(arrPara[i])
            }
        }
        this.setData({
            id: sceneData[0] ? sceneData[0] : (options.id ? options.id : ''),
            citycode: sceneData[1] ? sceneData[1] : (options.city ? options.city : (wx.getStorageSync("citycode") ? wx.getStorageSync("citycode") : 'sz')),
        })
        wx.setStorageSync('citycode', this.data.citycode)

        util.getUrl(app.globalData.url).then((res) => {
            // 获取城市公众号二维码
            util.cityConfig(this.data.citycode).then(res => {
                this.setData({
                    qrcodeimg: res.qrcode,
                })
            })
            util.cityList().then((res) => {
                let citydata;
                if (res.allcity && Array.isArray(res.allcity))
                    citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                if (citydata)
                    this.setData({
                        city: citydata[0].cityname
                    })
                wx.setStorageSync('city', this.data.city)
            })
            this.getsubTopicDetail(this.data.id);
        })

        // 获取屏幕高度
        wx.getSystemInfo({
            success: (res) => {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR;
                this.setData({
                    winHeight: calc,
                    winWidth: clientWidth * rpxR,
                });
            }
        });

        //获取胶囊位置信息
        let menuButtonObject = wx.getMenuButtonBoundingClientRect();
        this.setData({
            navtop: menuButtonObject.top,
            inputHeight: menuButtonObject.height,
        })
    },

    // 我要办理是否展开
    showItems() {
        this.setData({
            showItems: !this.data.showItems,
        })
    },

    // 子专题详情
    getsubTopicDetail(id) {
        let data = {
            action: "childDetail",
            id: id,
            citycode: this.data.citycode
        };
        util.request('zhuanti.php', data, ).then(res => {
            let subtopic = res.data.data;
            if (subtopic) {
                wx.setNavigationBarTitle({
                    title: subtopic.name
                });
                this.setData({
                    subtopic: subtopic,
                    subtitle: [subtopic.title1 ? subtopic.title1 : '', subtopic.title2, subtopic.title3, subtopic.title4],
                    subContent: [subtopic.article1, subtopic.article2, subtopic.article3, subtopic.article4],
                    subtopicName: subtopic.sname,
                });
                this.subtopicName = subtopic.sname;
                this.getBannerData(subtopic.name)
                this.getToolData(subtopic.name)
                this.getTopticlist(subtopic.pid, subtopic.name)
                this.getTopicDetail(subtopic.pid)
            }
        });
    },

    // 总专题详情
    getTopicDetail(id) {
        let data = {
            action: "detail",
            id: id,
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.setData({
                topicDetail: res.data.data,
            });

            this.getInfoList(this.data.topicDetail.hasOwnProperty('type') ? this.data.topicDetail.type : '', this.data.topicDetail.classid)
        })
    },

    // 子专题列表
    getTopticlist(pid, topicName) {
        let data = {
            action: "child",
            pid: pid,
        };
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data) {
                res.data.data.forEach((v, k) => {
                    if (v.name === topicName) {
                        v.showOption = true;
                    } else {
                        v.showOption = false;
                    }
                })
                this.setData({
                    subList: res.data.data,
                });
                let report = {
                    action: this.data.subList[0].sname,
                };
                if (util.getScene(wx.getLaunchOptionsSync().scene))
                    report.scene = util.getScene(wx.getLaunchOptionsSync().scene);
            }
        })
    },

    // 获取文章列表接口
    getInfoList(type, id) {
        let infoData = {
            citycode: (type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' : this.data.citycode,
            action: 'list',
            pagesize: 10,
            classid: id,
        };
        util.request('index.php', infoData, 'GET').then(res => {
            this.setData({
                    infoList: res.data,
                })
                // this.infoList = res.data;
        })
    },

    // 首页swiper-banner
    getBannerData(name) {
        let bannerData = {
            action: 'lunbo',
            city: this.data.city,
            block: "专题",
            blockvalue: name,
        };
        util.request('service.php', bannerData, ).then(res => {
            this.setData({
                bannerArray: res.data.data,
            })
        })
    },
    // 获取工具专题服务接口
    getToolData(toolname) {
        let toolData = {
            action: "tools",
            city: this.data.city,
            keywords: toolname,
            version: app.globalData.version,
            type: "zhuanti"
        };
        util.request('service.php', toolData, ).then(res => {
            this.setData({
                servList: res.data.data,
            })

            // 获取顶部的距离
            var query = wx.createSelectorQuery()
            query.select('.worklist-box').boundingClientRect((res) => {
                if (res != null) {
                    this.setData({
                        queryWidth: res.width
                    })
                }
                // 计算比例
                this.getRatio();
            }).exec()
        })
    },

    //根据分类获取比例
    getRatio() {
        if (!this.data.servList || this.data.servList.length <= 4) {
            this.setData({
                slideShow: false
            })
        } else {
            var _totalLength = this.data.servList.length * 170; //分类列表总长度
            //滚动列表长度与滑条长度比例
            // var _ratio = 230 / _totalLength * (750 / this.queryWidth);
            var _ratio = 100 / _totalLength * (750 / this.data.queryWidth);
            //当前显示红色滑条的长度(保留两位小数)
            var _showLength = 750 / _totalLength * 100;

            this.setData({
                slideWidth: _showLength,
                totalLength: _totalLength,
                slideShow: true,
                slideRatio: _ratio,
            })
        }
    },
    //slideLeft动态变化
    getleft(e) {
        this.setData({
            slideLeft: e.detail.scrollLeft * this.data.slideRatio
        })
    },

    menuClick(e) {
        let meunIndex = e.currentTarget.dataset.index;
        this.setData({
            meunIndex,
        })
    },

    // 我要办理
    subListClick(e) {
        let id = e.currentTarget.dataset.id;
        let name = e.currentTarget.dataset.name;
        let sname = e.currentTarget.dataset.sname;
        let report = {
            action: name,
        };

        if (util.getScene(wx.getLaunchOptionsSync().scene)) {
            report.scene = util.getScene(wx.getLaunchOptionsSync().scene);
            util.reportMessage("subtopic", report);
        }

        this.setData({
            servList: '',
            meunIndex: 0,
        })
        this.data.subList.forEach((v, k) => {
            if (v.sname == sname) {
                v.showOption = true;
                this.getsubTopicDetail(id)
            } else {
                v.showOption = false;
            }
        })
        this.setData({
            subList: this.data.subList,
        })
    },

    // banner点击事件
    bannerClick(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }
    },

    // 工具服务点击事件
    servClick(e) {
        let path = e.currentTarget.dataset.path;
        let appid = e.currentTarget.dataset.appid;
        let linkname = e.currentTarget.dataset.title;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: this.data.subtopic ? this.data.subtopic.name : "本地宝",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: this.data.subtopic ? this.data.subtopic.name : "本地宝",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            query: {
                id: this.data.id,
                city: this.data.citycode
            },
        }
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function(e) {
        this.setData({
            showNav: false,
            scrollTop: e.scrollTop,
        })
    }
})